<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="采购单号" prop="nos">
        <el-input v-model="queryParams.params['nos']" placeholder="请输入采购单号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="采购人" prop="lvBuycreateUsername">
        <el-input v-model="queryParams.lvBuycreateUsername" placeholder="请输入采购人"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="制单人" prop="createBy">
        <el-input v-model="queryParams.createBy" placeholder="请输入制单人"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="质检人" prop="zhijianName">
        <el-input v-model="queryParams.zhijianName" placeholder="请输入质检人"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="收货人" prop="shourenName">
        <el-input v-model="queryParams.shourenName" placeholder="请输入收货人"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="到货单号" prop="nos">
        <el-input v-model="queryParams.nos" placeholder="请输入到货单号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="到货日期">
        <el-date-picker v-model="dateRange"
                        style="display: flex;"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="供应商" prop="supplierName">
        <el-input v-model="queryParams.supplierName" placeholder="请输供应商"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
<!--      <el-form-item label="采购类型" prop="goodsType">-->
<!--        <el-select v-model="queryParams.params['goodsType']" placeholder="请选择采购类型">-->
<!--          <el-option v-for="dict in dict.type.goods_type" :key="dict.value" :label="dict.label" :value="dict.value" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="采购类型" prop="ywType">
        <el-select v-model="queryParams.params['ywType']" placeholder="请选择采购类型">
          <el-option v-for="dict in dict.type.caigou_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
              :max-height="tableMaxHeight" @sort-change="handleSortChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="采购单号" align="center" prop="lvBuy.nos"/>
      <el-table-column sortable label="到货单号" align="center" prop="nos"/>
      <el-table-column sortable label="到货日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.arrivalDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="采购人" align="center" prop="lvBuycreateUsername"/>
      <el-table-column sortable label="制单人" align="center" prop="createBy"/>
      <el-table-column sortable label="质检人" align="center" prop="zhijianName"/>
      <el-table-column sortable label="收货人" align="center" prop="shourenName"/>
      <!--<el-table-column sortable label="质检人" align="center" prop="nos"/>
      <el-table-column sortable label="收货人" align="center" prop="nos"/>-->
      <el-table-column sortable label="到货类型" align="center" prop="fahuoType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fahuo_type" :value="scope.row.fahuoType"/>
        </template>
      </el-table-column>
      <el-table-column sortable label="到货审核状态" align="center" prop="status" width="150">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.arrive_assess_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="供应商名称" align="center" prop="supplierName"/>
      <el-table-column sortable label="到货金额" align="center" prop="daohuoMoney"/>
      <el-table-column sortable label="到货重量" align="center" prop="dunwei"/>
      <el-table-column sortable label="运费" align="center" prop="yunfei"/>
      <el-table-column sortable label="采购类型" align="center" prop="goodsType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.caigou_type" :value="scope.row.lvBuy.ywType"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="直发货" prop="zhifahuo" show-overflow-tooltip>
        <template slot-scope="scope">
          <file-upload-show :value="scope.row.zhifahuo"></file-upload-show>
        </template>
      </el-table-column>
      <el-table-column sortable label="是否打款运费" align="center" prop="isSong"/>
      <el-table-column align="center" label="直发回执" prop="zhifahuo" show-overflow-tooltip>
        <template slot-scope="scope">
          <file-upload-show :value="scope.row.zhifahuo"></file-upload-show>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
        <template slot-scope="scope">
          <el-popconfirm :title="`确认采购弃审当前数据项？`" @confirm="handleBuyRefuse(scope.row)"
                         v-if="queryParams.status === '0'" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-error"
                       v-hasPermi="['develop:LvBuy:edit']">采购弃审
            </el-button>
          </el-popconfirm>

          <el-popconfirm :title="`确认质检弃审当前数据项？`" @confirm="handleTestRefuse(scope.row)"
                         v-if="queryParams.status === '0' || queryParams.status === '5'" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-error"
                       v-hasPermi="['develop:LvBuy:edit']">质检弃审
            </el-button>
          </el-popconfirm>

          <el-popconfirm :title="`确认质检通过当前数据项？`" @confirm="handleTestPass(scope.row)"
                         v-if="queryParams.status === '0' || queryParams.status === '5'" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-success"
                       v-hasPermi="['develop:LvBuy:edit']">采购质检通过
            </el-button>
          </el-popconfirm>

          <el-popconfirm :title="`确认库管入库当前数据项？`" @confirm="handleStorePass(scope.row)"
                         v-if="queryParams.status === '2'" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-circle-check"
                       v-hasPermi="['develop:LvBuy:edit']">库管入库
            </el-button>
          </el-popconfirm>

          <el-popconfirm :title="`确认库管弃审当前数据项？`" @confirm="handleStoreRefuse(scope.row)"
                         v-if="queryParams.status === '2'" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-circle-close"
                       v-hasPermi="['develop:LvBuy:edit']">库管弃审
            </el-button>
          </el-popconfirm>

          <el-popconfirm :title="`确认采购弃审当前数据项？`" @confirm="handleBuyRefuse(scope.row)"
                         v-if="queryParams.status === '3'" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-circle-close"
                       v-hasPermi="['develop:LvBuy:edit']">采购弃审
            </el-button>
          </el-popconfirm>

          <el-popconfirm :title="`确认入库退货当前数据项？`" @confirm="storeGoodsReturn(scope.row)"
                         v-if="queryParams.status === '4'" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-circle-close"
                       v-hasPermi="['develop:LvBuy:edit']">入库退货
            </el-button>
          </el-popconfirm>

          <el-popconfirm :title="`确认提交质检当前数据项？`" @confirm="handleStoreRefuse(scope.row)"
                         v-if="queryParams.status === '3'" style="margin: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-circle-check"
                       v-hasPermi="['develop:LvBuy:edit']">提交质检
            </el-button>
          </el-popconfirm>

          <el-button size="mini" type="text" icon="el-icon-document" style="margin-left: 10px;"
                     @click="daohuoDetail(scope.row)">到货明细
          </el-button>

          <el-button size="mini" type="text" icon="el-icon-document" style="margin-left: 10px;"
                     @click="daohuoDetailMerge(scope.row)">明细合并
          </el-button>

          <el-button size="mini" type="text" icon="el-icon-printer"
                     @click="handlePrint(scope.row)" v-hasPermi="['develop:PayApply:edit']">到货单
          </el-button>


<!--          <el-button size="mini" type="text" icon="el-icon-money" v-if="scope.row.yunfei>0" style="margin-left: 10px;"-->
<!--                     @click="yunfei(scope.row)">打款运费-->
<!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList"/>
    <lvBuy-addFormList ref="formCg" @ok="getList"></lvBuy-addFormList>
    <lv-daohuo-detail-list ref="daohuoDetailList" @ok="getList"></lv-daohuo-detail-list>
    <lv-daohuo-detail-merge-list ref="daohuoDetailMergeList" @ok="getList"></lv-daohuo-detail-merge-list>
    <yunfei-form ref="yunfeiform" @ok="getList"></yunfei-form>
    <lv-daohuo-detail-print ref="lvDaohuoDetailPrint" @ok="getList"></lv-daohuo-detail-print>

  </div>
</template>

<script>
import {deleteAction, getAction} from '@/api/manage'
import LvBuyAddFormList from '@/views/develop/lvBuy/module/LvBuyAddFormList'
import YunfeiForm from "@/views/develop/lvBuy/module/YunfeiForm";
import LvDaohuoDetailList from "@/views/develop/lvDaohuoDetail/module/LvDaohuoDetailList.vue";
import {list} from "@/api/develop/lvDaohuo";
import FileUploadShow from "@/components/FileUpload/FileUploadShow.vue";
import LvDaohuoDetailPrint from "@/views/develop/lvDaohuoDetail/module/LvDaohuoDetailPrint.vue";
import LvDaohuoDetailMergeList from "@/views/develop/lvDaohuoDetail/module/LvDaohuoDetailMergeList.vue";

export default {
  dicts: ['fahuo_type', 'arrive_assess_status','goods_type','caigou_type'],
  name: "DaohuoList",
  components: {
    LvDaohuoDetailMergeList,
    LvDaohuoDetailPrint,
    FileUploadShow,
    LvDaohuoDetailList,
    LvBuyAddFormList,
    YunfeiForm,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购单表格数据
      dataList: [],
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        mainId: null,
        nos: null,
        ddDate: null,
        subDate: null,
        type: null,
        ywType: null,
        tax: null,
        customerId: null,
        customerName: null,
        isBuy: null,
        dunwei: null,
        supplierId: null,
        supplierName: null,
        deptId: null,
        deptName: null,
        userId: null,
        userName: null,
        state: null,
        arrivalDate: null,
        lvBuycreateUsername: null,
        createBy: null,
        zhijianName: null,
        shourenName: null,
        orderByColumn: "a.arrival_date",
        isAsc: "desc",
        params: {}
      },
      url: {
        list: '/lvDaohuo/lvDaohuo/list',
        buyRefuse: '/lvDaohuo/lvDaohuo/buyRefuse',
        testPass: '/lvDaohuo/lvDaohuo/testPass',
        testRefuse: '/lvDaohuo/lvDaohuo/testRefuse',
        storePass: '/lvDaohuo/lvDaohuo/storePass',
        storeRefuse: '/lvDaohuo/lvDaohuo/storeRefuse',
        storeGoodsReturn: '/lvDaohuo/lvDaohuo/storeGoodsReturn',
        removeCg: '/lvBuy/lvBuy/removeCg/',
        export: '/lvBuy/lvBuy/export',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    this.queryParams.status = this.$route.query.status;
    this.getList();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    daohuoDetail(row) {
      this.$refs.daohuoDetailList.showDaohuoDetailListByDaohuo(row);
    },

    daohuoDetailMerge(row) {
      this.$refs.daohuoDetailMergeList.showDaohuoDetailListByDaohuo(row);
    },

    handlePrint(row) {
      this.$refs.lvDaohuoDetailPrint.showDetail(row);
    },
    /*zhijian(row){
      this.$refs.zhijianformList.zhijian(row);
    },*/
    yunfei(row) {
      this.$refs.yunfeiform.showyunfei(row);
    },
    zhifahuo(row) {
      this.$refs.zhifahuoform.showzhifahuo(row);
    },
    /** 查询采购单列表 */
    getList() {
      this.loading = true;
      list(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
        this.queryParams.params['ywType'] = '';
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    /* handleAdd() {
         this.$refs.form.handleAdd();
     },*/
    handleAddCg() {
      this.$refs.formCg.handleAddCg();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },
    handleDeleteCgd(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.removeCg, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(this.url.export, {
        ...this.queryParams
      }, `lvBuy_${new Date().getTime()}.xlsx`)
    },

    handleBuyRefuse(row) {
      getAction(this.url.buyRefuse + "/" + row.id, {}).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      }).catch(() => {
      });
    },

    handleTestPass(row) {
      getAction(this.url.testPass + "/" + row.id, {}).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      }).catch(() => {
      });
    },

    handleTestRefuse(row) {
      getAction(this.url.testRefuse + "/" + row.id, {}).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      }).catch(() => {
      });
    },

    handleStorePass(row) {
      getAction(this.url.storePass + "/" + row.id, {}).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      }).catch(() => {
      });
    },
    handleStoreRefuse(row) {
      getAction(this.url.storeRefuse + "/" + row.id, {}).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      }).catch(() => {
      });
    },

    storeGoodsReturn(row) {
      getAction(this.url.storeGoodsReturn + "/" + row.id, {}).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      }).catch(() => {
      });
    },








  }
};
</script>
